/*	kd:
	
	This is UI handling in the shop. You can imagine that this is a process in
	which you get item selection and stuff. The mouse will move and buttons will
	be pressed and such.
	
	*/

struct Gc_Shop_UiHandling {
	
	// kd: And all our buttons.
	Gc_UiButtons					buttons;
	
	// kd: Controls hovering inside of the ring.
	private int						current_item;
	private int						previous_item;
	private bool					is_inside_ring;
	private bool					was_inside_ring;
	private bool					can_select_items;
	
	// kd: This controls hovering by the mouse.
	private int						current_button;
	private int						previous_button;
	private bool					is_inside_button;
	private bool					was_inside_button;
	
	// kd: This controls both shortcuts and clicking buttons. There can be only
	// one, but that's good enough.
	private bool					has_acted;
	private int						current_action;
	
	// kd: Not private on purpose.
	Gc_MouseInput					mouse_input;
	
	enum button_names {
	
	// kd: These will be drawn over the text.
		top_layer		= 0
	,	scroll_down		= top_layer
	,	scroll_up
	,	top_layer_end
	
	// kd: And these below text.
	,	bottom_layer	= top_layer_end
	,	left			= bottom_layer
	,	right
	// ,	poke_keeper
	,	switch_keeper
	// ,	alpha_slider
	,	favorite
	,	fx
	,	button_count
	}
	
	enum extra_sprites {
		message_box	= button_count
	,	fx_on
	,	fx_off
	
	// kd: From here on, drawn additive.
	,	extra_additive
	,	favorite_off = extra_additive
	,	favorite_on
	,	total_sprite_count	// same thing!
	}
	
	bool Build () {
		
		// kd: Make sure the mouse works.
		mouse_input = new("Gc_MouseInput");
		
		if(!mouse_input) {
			return false;
		}
		
		// kd: Now get some space here, too. The good stuff happens later.
		buttons.ClearAndResize(button_count);
		buttons.SetShortcut(left, BT_MOVELEFT | BT_LEFT);
		buttons.SetShortcut(right, BT_MOVERIGHT | BT_RIGHT);
		buttons.SetShortcut(scroll_up, BT_FORWARD);
		buttons.SetShortcut(scroll_down, BT_BACK);

		return true;
	}
	
	void OnInit (
	vector2			cursor_resolution,
	vector2			sprite_resolution,
	Gc_UiSprites	sprites,
	Gc_UiAnim		move_anim) {
		mouse_input.Init(cursor_resolution, (0.5, 0.5));
		
		// kd: LOL
		current_item			= 0;
		previous_item			= 0;
		is_inside_ring			= false;
		was_inside_ring			= false;
		can_select_items		= false;
		
		// kd: And this'll do the trick.
		OnInitButtons(
			cursor_resolution,
			sprite_resolution,
			sprites,
			move_anim);
	}
	
	void OnInitButtons (
	vector2			cursor_resolution,
	vector2			sprite_resolution,
	Gc_UiSprites	sprites,
	Gc_UiAnim		move_anim) {
		buttons.InitFromUiAnim(
			move_anim,
			sprites,
			cursor_resolution,
			sprite_resolution);
	}
	
	void InitLayout (
	vector2			cursor_resolution,
	vector2			sprite_resolution) {
		mouse_input.Init(cursor_resolution, (0.5, 0.5));
		
		// kd: LOL
		current_item			= 0;
		previous_item			= 0;
		is_inside_ring			= false;
		was_inside_ring			= false;
		can_select_items		= false;
	}
	
	void NextState (PlayerInfo player_info, Gc_Shop_InvLayout ring_layout) {
		NextLayoutState(ring_layout);
		NextButtonState(player_info);
		EndState();
	}
	
	void EndState () {
		// kd: Prevent mouse flicker and allow uncapped fps.
		mouse_input.OutdatePosition();
	}
	
	void NextLayoutState (Gc_Shop_InvLayout ring_layout) {
		// kd: Store this as old state. This way we can find out if we just
		// selected an item and such.
		was_inside_ring		= is_inside_ring;
		previous_item		= current_item;
		was_inside_button	= is_inside_button;
		previous_button		= current_button;
		
		// kd: Now find a new item.
		let cursor_pos = mouse_input.Position();
		
		if(ring_layout.IsCursorInside(cursor_pos) && can_select_items) {
			is_inside_ring = true;
			current_item = ring_layout.IndexAtCursor(cursor_pos);
		}
		
		else {
			is_inside_ring = false;
		}
	}
	
	void NextButtonState (PlayerInfo player_info) {
		// kd: Find out what we're hovering over.
		let cursor_pos = mouse_input.Position();
		
		buttons.PrepareCursorCheck(cursor_pos);
		
		is_inside_button = buttons.IsHovered();
		
		if(is_inside_button) {
			current_button = buttons.FoundButton();
		}
		
		// kd: Find out if the player is pressing anything.
		let bt_press =
			player_info.original_cmd.buttons &
			(~player_info.original_oldbuttons);
		
		buttons.PrepareActionCheck(bt_press);
		
		// kd: We act either by pressing the shortcut or by using the cursor to
		// press the button.
		has_acted		= false;
		current_action	= -1;
		
		if(is_inside_button && mouse_input.HasAnyActed(player_info)) {
			has_acted		= true;
			current_action	= current_button;
		}
		
		if(buttons.HasActed()) {
			has_acted		= true;
			current_action	= buttons.FoundAction();
		}
	}
	
	void SetCanSelectItems (bool can_select_items_) {
		can_select_items = can_select_items_;
	}
	
	int CurrentItem () const {
		return current_item;
	}
	
	int PreviousItem () const {
		return previous_item;
	}
	
	bool IsInsideRing () const {
		return is_inside_ring;
	}
	
	bool HasHoveredItem () const {
		return
			!was_inside_ring && is_inside_ring ||	// just entered ring
			was_inside_ring && previous_item != current_item;	// found new
	}
	
	bool HasDehoveredItem () const {
		return
			was_inside_ring && !is_inside_ring ||	// just left ring
			was_inside_ring && previous_item != current_item;	// found new
	}
	
	bool HasTriedBuying (PlayerInfo player_info) const {
		return is_inside_ring && mouse_input.HasAltActed(player_info);
	}
	
	bool HasTriedEquipping (PlayerInfo player_info) const {
		return is_inside_ring && mouse_input.HasActed(player_info);
	}
	
	int CurrentButton () const {
		return current_button;
	}
	
	int PreviousButton () const {
		return previous_button;
	}
	
	bool HasHoveredButton () const {
		return
			!was_inside_button && is_inside_button ||
			was_inside_button && current_button != previous_button;
	}
	
	bool HasDehoveredButton () const {
		return
			was_inside_button && !is_inside_button ||
			was_inside_button && current_button != previous_button;
	}
	
	bool IsHoveringButton () const {
		return is_inside_button;
	}
	
	bool HasEnteredRing () const {
		return !was_inside_ring && is_inside_ring;
	}
	
	bool HasLeftRing () const {
		return was_inside_ring && !is_inside_ring;
	}
	
	bool HasActed () const {
		return has_acted;
	}
	
	int CurrentAction () const {
		return current_action;
	}
}

class Gc_MouseInput {
	// kd: You'll practically always need the old pos to do uncapped fps!
	private vector2					old_pos;
	private vector2					pos;
	private vector2					resolution;
	
	void Init (
	vector2		res			= (1, 1),
	vector2		normal_pos	= (0.5, 0.5)) {
		pos			= (normal_pos.x * res.x, normal_pos.y * res.y);
		old_pos		= pos;
		resolution	= res;
	}
	
	// kd: This is necessary cuz NextState is called from some UI mouse thingy
	// that really only calls when the mouse is moved. This prevents flicker!
	
	void OutdatePosition () {
		old_pos		= pos;
	}
	
	void NextState (int dx, int dy) {
		let offset	= 0.35 * (dx, -4 * dy);
		pos			= Gc_Vec.Restrain2(pos + offset, (0, 0), resolution);
	}
	
	vector2 Position () const {
		return pos;
	}
	
	ui vector2 DrawPos (double t = 1) const {
		return t * pos + (1 - t) * old_pos;
	}
	
	ui vector2 Space () const {
		return resolution;
	}
	
	const KEY_ACT		= BT_ATTACK;
	const KEY_ALTACT	= BT_ALTATTACK;
	const KEY_ANYACT	= KEY_ACT | KEY_ALTACT;
	
	bool HasActed (PlayerInfo player_info) const {
		return
			player_info.original_cmd.buttons &
			(~player_info.original_oldbuttons) &
			KEY_ACT;
	}
	
	bool HasAltActed (PlayerInfo player_info) const {
		return
			player_info.original_cmd.buttons &
			(~player_info.original_oldbuttons) &
			KEY_ALTACT;
	}
	
	bool HasAnyActed (PlayerInfo player_info) const {
		return
			player_info.original_cmd.buttons &
			(~player_info.original_oldbuttons) &
			KEY_ANYACT;
	}
}
